﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Telnet UI
 * Version 1.00
 * 
 * Font Selection Dialog Class
 *      Provides a dialog definition for displaying and selecting a font for display use.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Text;

using Adaptive.Foundation.Windows.Forms;
using Adaptive.Telnet.UI.Properties;

namespace Adaptive.Telnet.UI
{
    /// <summary>
    /// Provides a dialog definition for displaying and selecting a font for display use.
    /// </summary>
    public partial class FontSelectionDialog : ModalDialogBase
    {
        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public FontSelectionDialog()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }

            components = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets the selected font.
        /// </summary>
        /// <value>
        /// A <see cref="Font"/> instance if one is selected; otherwise, returns <b>null</b>.
        /// </value>
        public Font SelectedFont
        {
            get { return InternalCreateFont(); }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Creates and assigns the event handlers for the controls on the dialog.
        /// </summary>
        protected override void AssignEventHandlers()
        {
            //Buttons.
            OkButton.Click += new EventHandler(HandleOkClick);
            CloseButton.Click += new EventHandler(HandleCloseClick);
            BoldButton.Click +=new EventHandler(HandleBoldClick);
            ItalicButton.Click += new EventHandler(HandleItalicClick);
            UnderlineButton.Click +=new EventHandler(HandleUnderlineClick);

            FontList.SelectedIndexChanged +=new EventHandler(HandleIndexChange);
            SizeText.TextChanged +=new EventHandler(HandleSizeChanged);
        }
        /// <summary>
        /// Removes the event handlers for the controls on the dialog.
        /// </summary>
        protected override void RemoveEventHandlers()
        {
            //Buttons.
            OkButton.Click -= new EventHandler(HandleOkClick);
            CloseButton.Click -= new EventHandler(HandleCloseClick);
            BoldButton.Click -= new EventHandler(HandleBoldClick);
            ItalicButton.Click -= new EventHandler(HandleItalicClick);
            UnderlineButton.Click -= new EventHandler(HandleUnderlineClick);

            FontList.SelectedIndexChanged -= new EventHandler(HandleIndexChange);
            SizeText.TextChanged -= new EventHandler(HandleSizeChanged);
        }
        /// <summary>
        /// Sets the display properties for the dialog.
        /// </summary>
        protected override void SetFormDisplay()
        {
        }
        /// <summary>
        /// Sets the initial content for the dialog.
        /// </summary>
        protected override void SetInitialContent()
        {
            InstalledFontCollection list = new InstalledFontCollection();
            ListViewItem newItem = null;
            Settings appSettings = null;

            //Load the font list.
            foreach (FontFamily item in list.Families)
            {
                newItem = FontList.Items.Add(item.Name, 0);
            }
            list.Dispose();
            list = null;

            //Load the app settings.
            appSettings = new Settings();
            newItem = FontList.FindItemWithText(appSettings.DefaultFontName);
            if (newItem != null)
            {
                newItem.Selected = true;
                newItem.EnsureVisible();
            }
            SizeText.Text = appSettings.DefaultFontSize.ToString();
        }
        /// <summary>
        /// Validates the contents of the dialog.
        /// </summary>
        /// <returns>
        /// <b>true</b> if the item entries are valid; otherwise, returns <b>false</b>.
        /// </returns>
        public override bool ValidateChildren()
        {
            return (InternalCreateFont() != null);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        /// <summary>
        /// Handles the event when the user clicks the OK button.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleOkClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            if (ValidateChildren())
            {
                DialogResult = DialogResult.OK;
                Close();
            }
            else
                Cursor = Cursors.Default;
        }
        /// <summary>
        /// Handles the event when the user clicks the Cancel button.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleCloseClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            DialogResult = DialogResult.Cancel;
            Close();
        }
        /// <summary>
        /// Handles the event when the user clicks the Bold button.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleBoldClick(object sender, EventArgs e)
        {
            PreviewText.Font = InternalCreateFont();
        }
        /// <summary>
        /// Handles the event when the user clicks the Italic button.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleItalicClick(object sender, EventArgs e)
        {
            PreviewText.Font = InternalCreateFont();
        }
        /// <summary>
        /// Handles the event when the user clicks the Underline button.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleUnderlineClick(object sender, EventArgs e)
        {
            PreviewText.Font = InternalCreateFont();
        }
        /// <summary>
        /// Handles the event when the user selects a new item in the font list.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleIndexChange(object sender, EventArgs e)
        {
            PreviewText.Font = InternalCreateFont();
        }
        /// <summary>
        /// Handles the event when the user changes the size specification.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleSizeChanged(object sender, EventArgs e)
        {
            PreviewText.Font = InternalCreateFont();
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        /// <summary>
        /// Creates the font specifyied by the current dialog settings.
        /// </summary>
        /// <returns>
        /// A <see cref="Font"/> instance if successful; otherwise, returns <b>null</b>.
        /// </returns>
        private Font InternalCreateFont()
        {
            Font returnValue = null;
            string fontName = string.Empty;
            float size = 0;
            FontStyle style = FontStyle.Regular;

            if (FontList.SelectedItems.Count > 0)
            {
                fontName = FontList.SelectedItems[0].Text;
                style = GetFontStyle();
                size = (float)SizeText.Value;
                if (size == 0)
                    size = 1;
                try
                {
                    returnValue = new Font(fontName, size, style);
                }
                catch
                {
                    returnValue = null;
                }
            }
            return returnValue;
        }
        /// <summary>
        /// Calculates the font style from the current settings.
        /// </summary>
        /// <returns>
        /// A <see cref="FontStyle"/> enumeration.
        /// </returns>
        private FontStyle GetFontStyle()
        {
            FontStyle returnValue = FontStyle.Regular;

            if (BoldButton.Checked)
                returnValue |= FontStyle.Bold;
            if (ItalicButton.Checked)
                returnValue |= FontStyle.Italic;
            if (UnderlineButton.Checked)
                returnValue |= FontStyle.Underline;

            return returnValue;
            
        }
        #endregion
    }
}
